<template>
    <div class="content">
        <div v-if="!ifNext">
            <Form :label-width="200" :model="form" :rules="rules" ref="form">
                <FormItem label="试卷名称" prop="field1">
                    <Input
                        v-model="form.paperName"
                        :maxlength="20"
                        placeholder="校内唯一，1到20位"
                        style="width: 300px"
                    />
                </FormItem>
                <FormItem label="答题时长" prop="field2">
                    <Input
                        v-model="form.totalTime"
                        type="number"
                        placeholder="校内唯一，1到20位"
                        style="width: 300px"
                    />
                    <span>分钟</span>
                </FormItem>
            </Form>
            <div class="content">
                <div style="height:30px;line-height:30px">
                    <span>已选:{{selectedData.length}}</span>&nbsp;&nbsp;
                    <span>单选题:{{types[1]}}</span>&nbsp;&nbsp;
                    <span> 多选题：{{types[2]}}</span>&nbsp;&nbsp;
                    <span> 填空题：{{types[3]}}</span>&nbsp;&nbsp;
                    <span>判断题：{{types[4]}} </span>&nbsp;&nbsp;
                    <span> 简答题：{{types[5]}}</span>&nbsp;&nbsp;
                    <span> 商品归类题：{{types[6]}} </span>&nbsp;&nbsp;
                    <span> 案例分析题：{{types[7]}}</span>&nbsp;&nbsp;
                </div>
                <div class="clearfix">
                    <Form class="f-fl" inline :label-width="5">
                        <FormItem>
                            <Input
                                v-model="params.topic"
                                placeholder="题干"
                            ></Input>
                        </FormItem>
                        <FormItem>
                            <Select v-model="params.type" placeholder="题型" style="width:100px" clearable>
                                <Option v-for="(item, k) in questionType" :key="k" :value="k">{{ item }}</Option>
                            </Select>
                        </FormItem>
                        <FormItem>
                            <Select v-model="params.difficulty" placeholder="难度" style="width:100px" clearable>
                                <Option v-for="(item, k) in questionDifficulty" :key="k" :value="k">{{ item }}</Option>
                            </Select>
                        </FormItem>
                        <FormItem>
                            <Select v-model="params.schoolId" @on-change="changeFrom" placeholder="来源" style="width:100px" clearable>
                                <Option v-for="(item, k) in schoolList" :key="k" :value="item.id">{{ item.name }}</Option>
                            </Select>
                        </FormItem>
                        <FormItem>
                            <Button type="primary" @click="search">搜索</Button>
                        </FormItem>
                        <FormItem>
                            <Button @click="clearSearch">清空</Button>
                        </FormItem>
                    </Form>

                </div>
                <Table @on-selection-change="getSelected" :data="list" :columns="columns" border align="center">

                    <template slot-scope="{ row }" slot="sort">
                        <div>{{ row.sort }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="topic">
                        <div>{{ row.topic }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="type">
                        <div>{{ questionType[row.type] }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="difficulty">
                        <div>{{ row.difficulty==1?'简单':row.difficulty==2?'一般':row.difficulty==3?'困难':'' }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="score">
                        <div>{{ row.score }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="creator">
                        <div>{{ row.creator }}</div>
                    </template>
                    <template slot-scope="{ row }" slot="createTime">
                        <div>{{ row.createTime }}</div>
                    </template>
                </Table>
                <Pagination
                    :pageSize.sync="params.pageSize"
                    :current.sync="params.pageNum"
                    :total="params.total"
                    @change="queryList"
                />
            </div>
            <div class="submit-footer">
                <Button type="primary" @click="next">下一步</Button>
                <Button>取消</Button>
            </div>
        </div>
        <FormPaper v-else :data="selectedData" :bc="submit" :before="before" showBtns/>

    </div>
</template>
<script>
import FormPaper from './components/form-paper'
import FormSmart from './components/form-smart'
import { questionType, questionDifficulty } from '@/libs/enums'
import { schoolManageList } from '@/api/schoolManage'
import { questionList } from '@/api/questionManage'
import { addPaper, getOnePaper, updatePaper } from '@/api/paper'
export default {
    components: {
        // eslint-disable-next-line vue/no-unused-components
        FormSmart,
        FormPaper
    },
    inject: ['nextPageReload'],
    data () {
        return {
            // 题型
            types: {
                1: 0,
                2: 0,
                3: 0,
                4: 0,
                5: 0,
                6: 0,
                7: 0
            },
            ifNext: false,
            selectedData: [],
            schoolList: [],
            questionType,
            questionDifficulty,
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10,
                schoolName: '',
                type: '', // 题型
                difficulty: '', // 难度
                schoolId: '',
                onlySystem: '',
                topic: ''
            },
            columns: Object.freeze([
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '所属分类', slot: 'sort', align: 'center' },
                { title: '题干', slot: 'topic', align: 'center' },
                { title: '题型', slot: 'type', align: 'center' },
                { title: '难度', slot: 'difficulty', align: 'center' },
                { title: '分值', slot: 'score', align: 'center' },
                { title: '创建人', slot: 'creator', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' }
            ]),
            list: [],
            form: {
                paperName: '',
                totalTime: ''
            },
            rules: {
                paperName: [
                    { required: true, message: '试卷名称必填', trigger: 'blur' },
                    { min: 1, max: 20, message: '本院校内唯一，20个字以内', trigger: 'blur,change' }
                ],
                totalTime: [
                    { required: true, type: Number, message: '答题时长必填', trigger: 'blur,change' }
                ]
            }
        }
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        next () {
            // 分类
            this.ifNext = true
        },
        before () {
            this.getSchoolList()
            this.queryList('show')
            this.form.paperName = this.$route.query.paperName
            this.form.totalTime = this.$route.query.totalTime
        },
        submit (data) {
            if (this.$route.query.id) {
                /* let idArr = []
                data.map(item => {
                    idArr.push({ testId: item.testId, score: item.score })
                }) */
                let idArr = data.map(item => {
                    return { testId: item.testId, score: item.score }
                })
                var params = {
                    paperId: this.$route.query.id,
                    paperName: this.$route.query.paperName,
                    totalTime: this.$route.query.totalTime,
                    list: idArr
                }
                updatePaper(params).then(res => {
                    console.log(res)
                    if (res.data.code != '0000') {
                        this.$Message.error(res.data.data)
                        return
                    }
                    this.$Message.success('修改成功')
                    // 跳转到列表页
                    this.$router.push({
                        name: 'testPaperList',
                        query: {}
                    })
                    this.nextPageReload()
                })
            } else {
                let idArr = data.map(item => {
                    return { testId: item.testId, score: item.score }
                })
                let params = {
                    paperName: this.form.paperName,
                    totalTime: this.form.totalTime,
                    list: idArr
                }
                addPaper(params).then(res => {
                    console.log(res)
                    if (res.data.code != '0000') {
                        this.$Message.error(res.data.data)
                        return
                    }
                    this.$Message.success('增加成功')
                    // 跳转到列表页
                    this.$router.push({
                        name: 'testPaperList',
                        query: {}
                    })
                    this.nextPageReload()
                })
            }
        },
        getSelected (data) {
            console.log(data)
            let vm = this
            this.selectedData = data
            this.types = {
                1: 0,
                2: 0,
                3: 0,
                4: 0,
                5: 0,
                6: 0,
                7: 0

            }
            this.selectedData.map((item, index) => {
                vm.types[item.type] = (vm.types[item.type]) + 1
            })
        },
        changeFrom (id) {
            console.log(id)
            if (id == 1) {
                this.params.schoolId = ''
                this.params.onlySystem = 1
            } else {
                this.params.onlySystem = ''
            }
        },
        getSchoolList () {
            var obj = { 'total': 0, 'status': '', 'pageNum': 1, 'pageSize': 10, 'schoolName': '', 'enable': '' }
            schoolManageList(obj).then(res => {
                console.log(res.data.data.list)
                this.schoolList = res.data.data.list.map(item => {
                    return {
                        id: item.schoolId,
                        name: item.schoolName
                    }
                })
                this.schoolList.unshift({ id: 1, name: '系统后台' })
            })
        },
        queryList (type) {
            /* this.list = Array(10).fill(0).map((val, i) => {
                return {
                    schoolId: i,
                    schoolName: `教师账号${i}`,
                    contacts: `教师姓名${i}`,
                    tel: `联系方式${i}`,
                    createTime: `所属专业${i}`,
                    field1: `一般${i}`,
                    field2: i + 1
                }
            }) */
            let params = {
                difficulty: this.params.difficulty, // 难度
                onlySystem: this.params.onlySystem, // 只查询系统1
                pageNum: this.params.pageNum,
                pageSize: this.params.pageSize,
                schoolId: this.params.schoolId, // 为空则查询所有学校和系统
                sortId: '', // 分类ID
                topic: this.params.topic, // 题干
                type: this.params.type// 题型
            }
            // 获取试题列表
            questionList(params).then((res) => {
                console.log(res)
                this.types = {
                    1: 0,
                    2: 0,
                    3: 0,
                    4: 0,
                    5: 0,
                    6: 0,
                    7: 0

                }
                this.list = res.data.data.list
                this.params.pageNum = res.data.data.pageNum
                this.params.pageSize = res.data.data.pageSize
                this.params.total = res.data.data.total
                if (type == 'show') {
                    this.ifNext = false
                }
            })
        },
        clearSearch () {
            this.params.topic = ''
            this.params.difficulty = ''
            this.params.type = ''
            this.params.schoolId = ''
        }
    },
    mounted () {
        let id = this.$route.query.id
        if (id) {
            // this.form.paperName  = this.$route.query.paperName
            // this.form.totalTime = this.$route.query.totalTime
            getOnePaper({ paperId: id }).then(res => {
                this.selectedData = res.data.data
                this.ifNext = true
            })
        } else {
            this.getSchoolList()
            this.queryList()
        }
    }
}
</script>
<style lang="less" scoped>
.submit-footer {
    margin-top: 20px;
    padding-left: 10px;
}
</style>
